<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiLog
{
	private $_loging;
	
	private $_filename;
	
	public function __construct()
	{
		$this->_loging = Rti::config('config')->get('LOGING');
		
		if ($this->_loging)
		{
			$this->_filename = APPPATH . '/logs/log_' . date('Y_m_d') . '.php'; 
			
			if (! file_exists($this->_filename))
			{
				$ctx = "<?php if (! defined('APPPATH')) die('No direct script access allowed'); \n";
				file_put_contents($this->_filename, $ctx, FILE_APPEND);
			}
			
			$request_stamp = "\n// --- " . date('Y-m-d H:i:s') . "\n";
			file_put_contents($this->_filename, $request_stamp, FILE_APPEND);
		}
	}
	
	public function write($data, $mode = 'debug')
	{
		if (! $this->_loging)
		{
			return FALSE;
		}	
		
		$line = serialize($data);	

	    list($usec, $sec) = explode(" ", microtime());
	    $time_stamp = ((float)$usec + (float)$sec);
		
		file_put_contents($this->_filename, "\$log['$mode']['$time_stamp'] = '$line';\n", FILE_APPEND);
	}
	
	public function read($filename = '')
	{
		if (empty($filename))
		{
			$filename = $this->_filename;
		}

		$log = FALSE;
		
		if (file_exists($filename))
		{
			include $filename;
		}
		
		return $log;		
	}
}

/* End of file RtiLog.php */
/* Location: ./system/core/RtiLog.php */

